package com.elai.baseconfig.service.impl;

import java.util.List;

import com.elai.baseconfig.domain.EventTeamDO;
import com.elai.baseconfig.service.EventTeamService;
import com.elai.common.utils.DateUtils;
import com.elai.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.elai.baseconfig.mapper.BaseLesseeInfoMapper;
import com.elai.baseconfig.domain.BaseLesseeInfo;
import com.elai.baseconfig.service.IBaseLesseeInfoService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 租户信息Service业务层处理
 *
 * @author elai
 * @date 2022-04-09
 */
@Service
public class BaseLesseeInfoServiceImpl implements IBaseLesseeInfoService
{
    @Autowired
    private BaseLesseeInfoMapper baseLesseeInfoMapper;

    @Autowired
    private EventTeamService eventTeamService;
    /**
     * 查询租户信息
     *
     * @param id 租户信息ID
     * @return 租户信息
     */
    @Override
    public BaseLesseeInfo selectBaseLesseeInfoById(Long id)
    {
        return baseLesseeInfoMapper.selectBaseLesseeInfoById(id);
    }

    /**
     * 查询租户信息列表
     *
     * @param baseLesseeInfo 租户信息
     * @return 租户信息
     */
    @Override
    public List<BaseLesseeInfo> selectBaseLesseeInfoList(BaseLesseeInfo baseLesseeInfo)
    {
        return baseLesseeInfoMapper.selectBaseLesseeInfoList(baseLesseeInfo);
    }

    /**
     * 新增租户信息
     *
     * @param baseLesseeInfo 租户信息
     * @return 结果
     */
    @Transactional
    @Override
    public int insertBaseLesseeInfo(BaseLesseeInfo baseLesseeInfo)
    {
        baseLesseeInfo.setCreateTime(DateUtils.getNowDate());
        baseLesseeInfo.setCreateBy(SecurityUtils.getUsername());
        Integer code = (int) ((Math.random() * 9 + 1) * 100000);
        String lesseeId="lessee"+DateUtils.dateTimeNow()+code;
        baseLesseeInfo.setLesseeId(lesseeId);

        EventTeamDO team=new EventTeamDO();
        team.setLesseeId(lesseeId);
        team.setTeamCode("basepoint");
        team.setTeamName("全埋点");
        team.setStatus("Y");
        eventTeamService.insertEventTeam(team);

        EventTeamDO custom=new EventTeamDO();
        custom.setLesseeId(lesseeId);
        custom.setTeamCode("custom");
        custom.setTeamName("自定义事件");
        custom.setStatus("Y");
        eventTeamService.insertEventTeam(custom);
        return baseLesseeInfoMapper.insertBaseLesseeInfo(baseLesseeInfo);
    }

    /**
     * 修改租户信息
     *
     * @param baseLesseeInfo 租户信息
     * @return 结果
     */
    @Override
    public int updateBaseLesseeInfo(BaseLesseeInfo baseLesseeInfo)
    {
        baseLesseeInfo.setUpdateTime(DateUtils.getNowDate());
        baseLesseeInfo.setUpdateBy(SecurityUtils.getUsername());
        return baseLesseeInfoMapper.updateBaseLesseeInfo(baseLesseeInfo);
    }

    /**
     * 批量删除租户信息
     *
     * @param ids 需要删除的租户信息ID
     * @return 结果
     */
    @Override
    public int deleteBaseLesseeInfoByIds(Long[] ids)
    {
        return baseLesseeInfoMapper.deleteBaseLesseeInfoByIds(ids);
    }

    /**
     * 删除租户信息信息
     *
     * @param id 租户信息ID
     * @return 结果
     */
    @Override
    public int deleteBaseLesseeInfoById(Long id)
    {
        return baseLesseeInfoMapper.deleteBaseLesseeInfoById(id);
    }

    @Override
    public BaseLesseeInfo selectBaseLesseeInfoByLesseeId(String lesseeId) {
        return baseLesseeInfoMapper.selectBaseLesseeInfoByLesseeId(lesseeId);
    }
}
